﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using Google.API.Translate;

namespace domen
{
    public static class TranslationHelper
    {
        private static Encoding encoding;
        private static readonly Google.API.Translate.TranslateClient client;

        static TranslationHelper()
        {
            client = new Google.API.Translate.TranslateClient("http://belodent.by");
        }

        public static Encoding Encoding
        {
            get
            {
                return encoding ??
                       (encoding =
                        Encoding.GetEncodings().Where(e => e.Name.Contains("1251")).FirstOrDefault().GetEncoding());
            }
        }

        public static string Translate(this string str)
        {
            if (String.IsNullOrWhiteSpace(str))
            {
                return str;
            }
            string result = str;
            try
            {
                result = client.Translate(str, Language.Russian, Language.English);
            }
            catch (Exception exception)
            {
                Debug.Write(exception.Message);
            }
            return result;
        }


        public static Dictionary<string, string> ToDictionary(this List<string> sc)
        {
            if (sc.Count%2 != 0) throw new InvalidDataException("Broken dictionary");

            var dic = new Dictionary<string, string>();
            for (int i = 0; i < sc.Count; i += 2)
            {
                dic.Add(sc[i], sc[i + 1]);
            }
            return dic;
        }

        public static List<String> ToStringCollection(this Dictionary<string, string> dic)
        {
            var sc = new List<string>();
            foreach (var d in dic)
            {
                sc.Add((d.Key));
                sc.Add(d.Value);
            }
            return sc;
        }
    }
}